package com.shop.cloud.common.mybatis;


public class MainTest {
	
	
	public static void main(String[] args) {
		String  sql="select Log.id,Log.name From log Log";
		System.out.println(getLimitString(sql,2,3));
		
		String s="select count(1) from log Log"; 
        String[] str = s.split("\\s+");
        for (int i = 0; i < 3; i++) {
        	String  string=str[i];
        	if(string.contains("count")){
        		System.out.println("true");
        	}
            System.out.println(string);
		}
	}
	
	private static int getAfterSelectInsertPoint(String sql) {
		int selectIndex = sql.toLowerCase().indexOf( "select" );
		final int selectDistinctIndex = sql.toLowerCase().indexOf( "select distinct" );
		return selectIndex + ( selectDistinctIndex == selectIndex ? 15 : 6 );
	}
	
	private static int getAfterSelectFromPoint(String sql) {
		return sql.toLowerCase().indexOf( "from" );
	}

	public static String getLimitString(String querySelect, int pageNo,int pageSize) {
		int a=getAfterSelectInsertPoint(querySelect);
		int b=getAfterSelectFromPoint(querySelect);
		StringBuffer string= new StringBuffer( querySelect)
				.insert( b, " from ( SELECT ROW_NUMBER() OVER (ORDER BY id) AS RowNumber, "+querySelect.substring(a, b) )
				.append(") as A  WHERE RowNumber > "+pageSize+"*("+pageNo+"-1)");
		return string.delete(a, b-1).insert( a, " top " + pageSize +" * ").toString();
	}

}
